//输入框
//用法：显示输入框，隐藏输入框
var inputBox = {
	//callback should be like: function(value)
	Show : function(text, defaultvalue, parentNode, callback){
		this.Hide(parentNode);
		
		var inputboxLayer = new InputBoxLayer(false, true);
		inputboxLayer.showDuration = 0.8;
		inputboxLayer.maxOpacity = 192;
		inputboxLayer.SetParam(text, defaultvalue, callback);
		inputboxLayer.setName("InputBox");
		inputboxLayer.show();
		
		parentNode.addChild(inputboxLayer);
	},

	Hide : function(parentNode){
		var oldbox = parentNode.getChildByName("InputBox");
		if (oldbox != null){
			oldbox.removeFromParent();
		}
	},
};

var InputBoxLayer = CanvasLayer.extend({
	
	tipLabel : null,
	
	inputbox : null,
	
	okCallback : function(value){
	},
	
	_OnOK : function(sender){
		this.onClose();
		this.okCallback(this.inputbox.getString());
	},
	
	ini : function(){
		var winSize = cc.director.getWinSize();
		
		this.tipLabel = cc.LabelTTF.create("", "simhei", 48);
		this.tipLabel.attr({anchorX : 0.5, anchorY : 0});
		this.node.addChild(this.tipLabel);
		
		var scale9sprite = new cc.Scale9Sprite(resource.scale9white100100_png, cc.rect(0,0,100,100), cc.rect(8,8,84,84));
		this.inputbox = new cc.EditBox(cc.size(winSize.width * 0.45, winSize.height * 0.12), scale9sprite);
		this.inputbox.setFontColor(cc.color(0, 0, 0));
		this.inputbox.setFont("simhei", 48);
		this.inputbox.setDelegate(this);
		this.node.addChild(this.inputbox);
		
		var OKMenu = this._CreateShortButton("确定", this._OnOK);
		var menu = cc.Menu.create(OKMenu);
		menu.attr({x:0,y:0});
		this.node.addChild(menu);
		
		ArrangeVertical(winSize.width * 0.5, winSize.height * 0.5, winSize.height * 0.08, [this.tipLabel, this.inputbox, OKMenu]);
	},

	_CreateShortButton : function(text,callback){
		var button = new cc.MenuItemImage(resource.ctrlbtnNormalS_png,resource.ctrlbtnDownS_png, resource.ctrlbtnDisableS_png, callback, this);
		button.setScale(1.5);
		var label = cc.LabelTTF.create(text, "simhei", 42);
		label.setPosition(button.width * 0.5, button.height * 0.5);
		label.setColor(cc.color(0,0,0));
		label.setScale(0.5);
		button.addChild(label);
		return button;
	},
	
	SetParam : function(text, defaultvalue, callback){
		this.tipLabel.setString(text);
		this.inputbox.setString(defaultvalue);
		this.okCallback = callback;
	}
});